<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%-- <link rel="stylesheet"
	href="<%=request.getContextPath() %>/WEB-ROOT/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
	href="<%=request.getContextPath() %>/WEB-ROOT/css/common.css">
<link rel="stylesheet"
	href="<%=request.getContextPath() %>/WEB-ROOT/bootstrap/css/treeview.css"> --%>


<%
HttpSession s = request.getSession(); 
%>
<body class="full" id="content">
	<div id="alert"></div>
	<div id="addWageList"></div>
	<div class="row">

		<div class="col-lg-12 col-md-12 col-sm-12 ">
		<form class="form-inline" role="form" id="selectDepVal">
				<div class="row">
					<div class="form-group inputselect col-lg-4">
						<input  type="file" id="file" name="file" accept="xls/xlsx"><br>
					</div>
					<div class="form-group inputselect col-lg-4">
					   <sec:authorize access="hasRole('ROLE_ADMIN')">
						   <button class="btn btn-info btn-sm " type="button" onclick="uploadFile()" class="btn btn-primary" data-dismiss="modal"
								   id="submitFile"><span class="glyphicon glyphicon-user">导入信息</span></button>
							<button type="button" class="btn btn-info btn-sm " id="addWage">
									<span class="glyphicon glyphicon-user"></span> 新增员工薪资
							</button>
						 </sec:authorize>
					</div>
				</div>
			</form>
			<form class="form-inline" role="form"></form>
			<div class="table-responsive " style="margin-left: 3%;">
				<table class="table table-hover tablefont">
					<tbody id="tableList">
					</tbody>
				</table>
				<div style="bottom: 10%">
					<div id="paging" style="text-align: center; margin-left: 25%;">
					</div>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
	//增加
	function uploadFile() {
		var file = $("#file").val();
		file = file.substring(file.lastIndexOf('.'), file.length);
		if (file == '') {
			alert("上传文件不能为空！");
		} else if (file != '.xlsx' && file != '.xls') {
			alert("请选择正确的excel类型文件！");
		} else {
			ajaxFileUpload();
		}
	}
	function ajaxFileUpload() {
		var formData = new FormData();
		var name = $("#file").val();
		formData.append("file", $("#file")[0].files[0]);
		formData.append("name", name);
		$.ajax({
			url: "importShort",
			type: "POST",
			async: false,
			data: formData,
			processData: false,
			contentType: false,
			beforeSend: function () {
				console.log("正在进行，请稍候");
			},
			success: function (e) {
				if (e == "01") {
					alert("导入成功");
					var file = document.getElementById('file');
					file.value = ''; //虽然file的value不能设为有字符的值，但是可以设置为空值
//或者
					file.outerHTML = file.outerHTML; //重新初始化了file的html
				} else {
					alert("导入失败");
				}

			}

		});
		initPage("0",pageSize,dataUrl);
	}
	var authority = "${authority}";
	var usrname = "${username}";
		//增加
		$("#addWage").click(function(){
			 $("#addWageList").load("WEB-ROOT/html/addWage.jsp",function(){
				$("#addModel").modal({
	     	        keyboard: true
	     	    });
		})
		});
		var pageSize = 8;
		var dataUrl = "${pageContext.request.contextPath}/wage";
		selectList= function(){
			initPage("0",pageSize,dataUrl);
		};
		initPage("0", pageSize, dataUrl);
		function initPage(num, ps, url) {
			//alert(authority);
			$.ajax({
				url : url,
				type : "GET",
				data : "currentPage=" + num + "&pageSize=" + ps ,
				contentType : "application/json;charset=utf-8",
				success : function(msg) {
					//alert(data.username);
					initTable(msg);
					if("[ROLE_USER]"==authority){
						 var mi=1;
						 while($('#tableList tr:eq('+ mi + ') td:eq(1)').text()!=""){
							 if($('#tableList tr:eq('+ mi + ') td:eq(1)').text()!=usrname){
									$('#tableList tr:eq('+ mi + ')').text("");
							 }
							 mi++;
						}   
						$("#paging").attr("style","display:none;");
					}
				},
				error : function(e) {
					console.log(e)
					alert("失败");
				}
			});
		}
		//初始化表格
		function initTable(data) {
			var obj = data.extend.data.list;
			var page = data.extend.data;
			page.pageNumber=page.pageNum;
			//table
			myTable = $.lTable(
					'#tableList',
					{
						data : obj
						,
						title : [
										"id",
										"account",
										"username",
										"departments",
										"bsalary",
							            "tardinesspenalty",
							            "earlyretirementpenalty",
							             "performancereward",
							             "overtimereward",
							             "allowanceforhightemperature",
							             "trafficsubsidy",
							              "rentalsubsidy",
							               "premium",
							"<button  class='btn btn-info btn-sm editDepa'  ID='editDepa' onclick='updF(id)'><span class='glyphicon glyphicon-user'></span> 编辑</button> <button  class='btn btn-info btn-sm delDepa' ID='delDepa' onclick='delF(id)'><span class='glyphicon glyphicon-user'></span>删除</button>" ] ,
								name : ["id", "员工编号", "员工姓名", "所属部门", "基本薪资","迟到罚款","早退罚款","绩效奖励","加班奖励","高温补贴","交通补贴","租房补贴","其他薪资","_opt" ],
										
								tid : "id",
								checkBox : "id"
							});
			
			$.lPaging('#paging',
			{
				pageNumber : page.pageNumber
				,

				totalPage : page.pages
				,
				countSize : 8
				,
				count : page.total,

				inputSearch : false
				,
				onPageChange : function(num) {
					initPage(num, pageSize, dataUrl);
				}
			});
		} 
		//编辑
				function updF(id) {
				$.ajax({
						type : "POST",
						url : "${pageContext.request.contextPath}/wageSelect",
						data : id,
						async : true,
						// dataType:"json",   
						contentType : "application/json;charset=utf-8",
						success : function(msg) {
							 $("#addWageList").load(
										"WEB-ROOT/html/addWage.jsp",
										function() {
											$("#addModel").modal({
												keyboard : true
											});
											var newUpdateDate = new Date();
											var wage = msg.extend.wage;
											if (wage != null) {
												$("#id").val(
														wage.id);
												$("#account").val(
														wage.account);
												$("#username").val(
														wage.username);
												$("#departments").val(
														wage.departments);
												$("#bsalary").val(
														wage.bsalary);
												$("#premium").val(
														wage.premium);
												$("#tardinesspenalty").val(
														wage.tardinesspenalty);
												$("#earlyretirementpenalty").val(
														wage.earlyretirementpenalty);
												$("#performancereward").val(
														wage.performancereward);
												$("#overtimereward").val(
														wage.overtimereward);
												$("#allowanceforhightemperature").val(
														wage.allowanceforhightemperature);
												$("#trafficsubsidy").val(
														wage.trafficsubsidy);
												$("#rentalsubsidy").val(
														wage.rentalsubsidy);
												$("#account").attr(
														"readOnly", true);
												$("#username").attr(
														"readOnly", true);
												$("#departments").attr(
														"readOnly", true);
												$("#submitType").val("PUT")
											}
											getSelectInput();
										})
						},
						error : function(msg) {
							alert("bb");
						}
					});

			}
			
			function delF(id) {
			$("#alert").load("WEB-ROOT/common/alert.jsp",
					function(response, status, xhr) {
						$('#alert').html(response);
					});
			$.ajax({
						type : "DELETE", 
						url : "${pageContext.request.contextPath}/delWage",
						data : id,
						async : true,
						//  dataType:"json",   
						contentType : "application/json;charset=utf-8",
						success : function(msg) {
							$("#alertText").text(msg.message);
							$("#alertModel").modal({
								keyboard : true
							});

						},
						error : function(msg) {
							$("#alertText").text("删除失败");
							$("#alertModel").modal({
								keyboard : true
							});
						}
					});
			}
	</script>
</body>





